﻿package com.codingbros.particles 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import flash.events.Event;
	
	import flash.geom.ColorTransform;
	
	public class ParticleSystem extends Sprite
	{		
		protected var systemWidth:int;
		protected var systemHeight:int;
		
		protected const PARTICLEMAXSPEED:Number = 3.0;
		
		protected static const PATICLESMAXNUM:int = 1000;
		protected var particlesNum:int;
		protected var particles:Vector.<Particle>;
		
		protected var particlesCanvas:Shape;
		
		protected var particlesBackgroundColor:uint = 0xffffff;
		protected var particlesBackground:Shape;
		
		protected var particlesBitmap:Bitmap;
		protected var particlesBitmapData:BitmapData;
		
		public function ParticleSystem(width:int, height:int) 
		{
			systemWidth = width;
			systemHeight = height;
			
			//particlesNum = PATICLESMAXNUM;
			numberOfParticles = PATICLESMAXNUM;
			particles = new Vector.<Particle>(particlesNum, true);
			
			var p:Particle;
			
			for (var i:int = 0; i < particlesNum; i ++) 
			{
				p = new Particle();
				
				p.x = i / particlesNum * systemWidth;
				p.y = 0;
				
				p.vy = Math.random() * PARTICLEMAXSPEED;
								
				particles[i] = p;
			}		
			
			particlesBitmapData = new BitmapData(systemWidth, systemHeight, true, 0xffffff);
			
			particlesBitmap = new Bitmap(particlesBitmapData);
			particlesBitmap.smoothing = true;
			this.addChild(particlesBitmap);
			
			particlesCanvas = new Shape();
			
			particlesBackground = new Shape();
			particlesBackground.graphics.beginFill(particlesBackgroundColor, 1);
			particlesBackground.graphics.drawRect(0, 0, systemWidth, systemHeight);
			particlesBackground.graphics.endFill();
				
		}
		
		public function init():void {
			this.addEventListener(Event.ENTER_FRAME, updateSystem);					
		}
		
		public function set numberOfParticles(num:int):void 
		{
			if (num < PATICLESMAXNUM) 
			{
				particlesNum = num;
			} else {
				particlesNum = PATICLESMAXNUM;
			}
		}
		
		protected function updateSystem(e:Event):void 
		{			
			
			particlesCanvas.graphics.clear();			
			particlesCanvas.graphics.lineStyle(1, 0x000000);
			
			var p:Particle;
			
			for (var i:int = 0; i < particlesNum; i ++) 
			{
				p = particles[i];
				
				particlesCanvas.graphics.moveTo(p.x, p.y);
				
				p.x += p.vx;
				p.y += p.vy;
				
				particlesCanvas.graphics.lineTo(p.x, p.y);	
				
				if (p.x <= 0 || p.x > systemWidth || p.y < 0 || p.y > systemHeight) 
				{
					p.x = i / particlesNum * systemWidth;
					p.y = 0;
				
					p.vy = Math.random() * PARTICLEMAXSPEED;
					//p.vx = Math.random() * PARTICLEMAXSPEED;
				}
			}
			
			particlesBitmapData.colorTransform(particlesBitmapData.rect, new ColorTransform( 1, 1, 1, 1, 0, 0, 0, -1 ) );
			//particlesBitmapData.draw(particlesBackground);
			particlesBitmapData.draw(particlesCanvas);
			
		}		
	}	
}